const app = getApp();
import { getStorage,setStorage } from "../../../utils/localStorage";
import { getMapDistance } from "../../../utils/address";
import { baseUrl } from "../../../api/request";
import { routeRecomtion_Info, commentRoute_Add, commentRoute_List } from "../../../api/luxian";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //navBar组件参数
    navData: {
      title: '路线信息',
      level: 2,
    },
    //图片地址
    baseUrl: baseUrl,
    //用户信息
    userInfo: {},
    //经纬度数据
    latlng: {},
    //自定义导航栏高度
    pageTop: app.globalData.navHeight + app.globalData.navTop + 10,
    detailedData: {},
    //评论列表
    commentList: [],
    //路线id
    travelId: null,
    //评论内容
    content: null,
    //是否显示评论列表
    commentStatus: false,
    wzState: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo: getStorage("user"),
      latlng: getStorage("latLng"),
    });
    if (JSON.stringify(options) && options.id) {
      this.setData({ travelId: options.id });
      this.getInfo();
      this.getCommentList();
    } else {
      wx.showToast({
        icon: 'none',
        title: '详情ID获取失败',
        duration: 2000
      });
    };
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  //获取详情数据
  getInfo() {
    routeRecomtion_Info({ travelId: this.data.travelId }).then(res => {
      let { data } = res.data;
      data.distance = getMapDistance(this.data.latlng.lat, this.data.latlng.lng, data.startLatitude, data.startLongitude);
      data.image = data.image ? data.image.split(",") : [];
      this.setData({
        detailedData: res.data.data
      });
    });
  },

  //地址点击事件
  addressTap() {
    setStorage("pageId", "list");
    wx.navigateTo({
      url: '/pagesLuxian/pages/guihuaComplete/index?id=' + this.data.detailedData.travelId,
    });
  },

  //获取评论列表
  getCommentList() {
    commentRoute_List({ travelId: this.data.travelId }).then(res => {
      this.setData({ commentList: res.data.data });
    });
  },

  //乡镇点击事件
  townListTap(e) {
    const item = e.currentTarget.dataset.item;
    console.log(item, 'item1');
    wx.navigateTo({
      url: '/pagesShouYe/pages/xaingcunDetail/index?id=' + item.villageId,
    });
  },
  //评论内容input框bindinput事件
  commentInput(e) {
    const value = e.detail.value;
    this.setData({ content: value });
  },
  //查看全部按钮点击事件
  allTap(e) {
    const status = e.currentTarget.dataset.status;
    this.setData({ commentStatus: !status });
  },
  //提交评论按钮点击事件
  commentSubmitTap() {
    if (this.data.content) {
      commentRoute_Add({ travelId: this.data.travelId, content: this.data.content, userId: this.data.userInfo.id }).then(res => {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
          duration: 2000
        });
        setTimeout(() => {
          this.setData({ content: null });
          this.getCommentList();
        }, 2000)
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '请输入评论内容',
        duration: 2000
      });
    };
  },

  //展开按钮点击事件
  zanKaiTap(e) {
    const state = e.currentTarget.dataset.state;
    this.setData({ wzState: !state });
  },

  /* 图片放大功能 */
  imgTap(e) {
    const url = e.currentTarget.dataset.url,
      urlList = e.currentTarget.dataset.urllist;
    console.log(url, urlList);
    if (url) {
      const arr = urlList.map(item => this.data.baseUrl + item);
      app.imagePreview(url, arr);
    };
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})